local jijuan = fk.CreateSkill{
  name = "plum__jijuan",
}

Fk:loadTranslationTable{
  ["plum__jijuan"] = "集卷",
  [":plum__jijuan"] = "记录你最近三张使用的非转化普通锦囊牌。你可以将任意张花色与被记录牌花色均不同的牌当做一张被记录牌使用，然后视为未使用过此牌。",

  ["#plum__jijuan"] = "集卷：将任意张牌当一张记录过的锦囊使用（不可使用花色：%arg）",
  ["@$plum__jijuan"] = "集卷",

  ["$plum__jijuan1"] = "圣人作《易》，顺性命之理，立三才之道。",
  ["$plum__jijuan2"] = "先天地，本阴阳，观九六之动。",
}

local U = require("packages/utility/utility")

jijuan:addLoseEffect(function (self, player)
  local room = player.room
  room:setPlayerMark(player, "@$plum__jijuan", 0)
  room:setPlayerMark(player, jijuan.name, 0)
end)

jijuan:addEffect("viewas", {
  anim_type = "special",
  pattern = ".|.|.|.|.|normal_trick",
  prompt = function (self, player)
    local suits = {}
    for _, id in ipairs(player:getTableMark("@$plum__jijuan")) do
      table.insertIfNeed(suits, Fk:getCardById(id):getSuitString(true))
    end
    return "#plum__jijuan:::"..table.concat(table.map(suits, Util.TranslateMapper))
  end,
  interaction = function (self, player)
    local all_names = table.map(player:getTableMark("@$plum__jijuan"), function (id) return Fk:getCardById(id).name end)
    local names = player:getViewAsCardNames(jijuan.name, all_names)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function (self, player, to_select)
    return not table.find(player:getTableMark("@$plum__jijuan"), function (id)
      return Fk:getCardById(id):compareSuitWith(Fk:getCardById(to_select))
    end)
  end,
  view_as = function (self, player, cards)
    if #cards == 0 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = jijuan.name
    card:addSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    local mark = player:getTableMark(jijuan.name)
    for i = #mark, 1, -1 do
      if Fk:getCardById(mark[i]).name == use.card.name then
        table.remove(mark, i)
        break
      end
    end
    if #mark == 0 then
      room:setPlayerMark(player, jijuan.name, 0)
      room:setPlayerMark(player, "@$plum__jijuan", 0)
    else
      room:setPlayerMark(player, jijuan.name, mark)
      if #mark < 4 then
        room:setPlayerMark(player, "@$plum__jijuan", mark)
      else
        local mark2 = {}
        for i = #mark, #mark - 2, -1 do
          table.insert(mark2, 1, mark[i])
        end
        room:setPlayerMark(player, "@$plum__jijuan", mark2)
      end
    end
  end,
  enabled_at_play = function (self, player)
    if player:getMark("@$plum__jijuan") == 0 then return end
    for _, id in ipairs(player:getTableMark("@$plum__jijuan")) do
      local card = Fk:cloneCard(Fk:getCardById(id).name)
      card.skillName = jijuan.name
      if player:canUse(card) and not player:prohibitUse(card) then
        return true
      end
    end
  end,
  enabled_at_response = function(self, player, response)
    if response or player:getMark("@$plum__jijuan") == 0 or player:isNude() then return end
    for _, id in ipairs(player:getTableMark("@$plum__jijuan")) do
      local card = Fk:cloneCard(Fk:getCardById(id).name)
      card.skillName = jijuan.name
      if (Fk.currentResponsePattern == nil and player:canUse(card) and not player:prohibitUse(card)) or
        Exppattern:Parse(Fk.currentResponsePattern):match(card) then
        return true
      end
    end
  end,
})

jijuan:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(jijuan.name, true) and data.card:isCommonTrick() and not data.card:isVirtual()
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:addTableMark(player, "plum__jijuan", data.card.id)
    local mark = player:getTableMark("@$plum__jijuan")
    table.insert(mark, data.card.id)
    if #mark > 3 then
      table.remove(mark, 1)
    end
    room:setPlayerMark(player, "@$plum__jijuan", mark)
  end,
})

return jijuan